# delimit;

* This file creates bal variables needed from extracted files;
* It needs crtr_attr166-crtr_attr186;
* balance variables;

* Our naming convention is: measure_account_delinquency;


* Replace missing balance variables with zeroes;
* Replace values coded as missing with missing values;
forvalues v = 166/187 {;
  replace crtr_attr`v' = 0 if crtr_attr`v' == . & num_all_tot ~= .;
  replace crtr_attr`v' = . if crtr_attr`v' >= 9999990;
};

* Balance variables;

rename crtr_attr167 bal_autof_tot;
rename crtr_attr168 bal_autob_tot;
rename crtr_attr169 bal_bankc_tot;
rename crtr_attr170 bal_consf_tot;
rename crtr_attr171 bal_fmrtg_tot;
rename crtr_attr172 bal_retal_tot;
rename crtr_attr173 bal_helin_tot;
rename crtr_attr174 bal_helre_tot;
rename crtr_attr176 bal_other_tot;

* this no longer works since student loans variable is messed up;
*rename crtr_attr166 bal_all_tot;
*rename crtr_attr175 bal_stdln_tot;

* now we need to make our own total balance variable which either excludes student loans or is only available from 2003 - present;
* here is the version that excludes student loans;
gen bal_all_tot =  bal_autof_tot + bal_autob_tot + bal_bankc_tot + bal_consf_tot + bal_fmrtg_tot + bal_retal_tot + bal_helin_tot + bal_helre_tot + bal_other_tot;

gen bal_allwstdln_tot = bal_stdln_tot + bal_autof_tot + bal_autob_tot + bal_bankc_tot + bal_consf_tot + bal_fmrtg_tot + bal_retal_tot + bal_helin_tot + bal_helre_tot + bal_other_tot;


*rename crtr_attr177 hic_all_tot;
rename crtr_attr178 hic_autof_tot;
rename crtr_attr179 hic_autob_tot;
rename crtr_attr180 hic_bankc_tot;
rename crtr_attr181 hic_consf_tot;
rename crtr_attr182 hic_fmrtg_tot;
rename crtr_attr183 hic_retal_tot;
rename crtr_attr184 hic_helin_tot;
rename crtr_attr185 hic_helre_tot;
*rename crtr_attr186 hic_stdln_tot;
rename crtr_attr187 hic_other_tot;

compress;

gen hic_auto_tot = hic_autof_tot + hic_autob_tot;
gen hic_bcret_tot = hic_bankc_tot + hic_retal_tot;
gen hic_home_tot = hic_fmrtg_tot + hic_helin_tot + hic_helre_tot;
gen hic_hel_tot = hic_helin_tot + hic_helre_tot;
gen hic_exhome_tot = hic_auto_tot + hic_bcret_tot + hic_consf_tot + hic_other_tot;
gen hic_other2_tot = hic_consf_tot + hic_other_tot;


foreach del in tot {;
  gen bal_auto_`del' = bal_autof_`del' + bal_autob_`del';
  gen bal_bcret_`del' = bal_bankc_`del' + bal_retal_`del';
  gen bal_home_`del' = bal_fmrtg_`del' + bal_helin_`del' + bal_helre_`del';
  gen bal_hel_`del' = bal_helin_`del' + bal_helre_`del';
  gen bal_exhome_`del' = bal_auto_`del' + bal_bcret_`del' + bal_consf_`del' + bal_other_`del';
  gen bal_other2_`del' = bal_consf_`del' + bal_other_`del';
};

compress;

